package com.example.benefitcommunity.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.example.benefitcommunity.dao.BcUserDao;
import com.example.benefitcommunity.entity.BcAddressList;
import com.example.benefitcommunity.entity.BcNotificationAnnouncement;
import com.example.benefitcommunity.dao.BcNotificationAnnouncementDao;
import com.example.benefitcommunity.entity.BcUser;
import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.enums.StatusCode;
import com.example.benefitcommunity.service.BcNotificationAnnouncementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通知公告表(BcNotificationAnnouncement)表服务实现类
 *
 * @author makejava
 * @since 2025-08-20 16:11:33
 */
@Transactional(rollbackFor = Exception.class)
@Service("bcNotificationAnnouncementService")
public class BcNotificationAnnouncementServiceImpl implements BcNotificationAnnouncementService {
    @Resource
    private BcNotificationAnnouncementDao bcNotificationAnnouncementDao;

    @Resource
    private BcUserDao bcUserDao;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BcNotificationAnnouncement queryById(Integer id) {
        return this.bcNotificationAnnouncementDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bcNotificationAnnouncement 筛选条件
     * @param pageRequest                分页对象
     * @return 查询结果
     */
    @Override
    public Page<BcNotificationAnnouncement> queryByPage(BcNotificationAnnouncement bcNotificationAnnouncement, PageRequest pageRequest) {
        long total = this.bcNotificationAnnouncementDao.count(bcNotificationAnnouncement);
        return new PageImpl<>(this.bcNotificationAnnouncementDao.queryAllByLimit(bcNotificationAnnouncement, pageRequest), pageRequest, total);
    }

    /**
     * @param title       姓名
     * @param communityid
     * @param pagenum     页码
     * @param pagesize    页大小
     * @return
     */
    @Override
    public Result<Object> queryByNotificationAnnouncement(String title, Integer communityid, Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum, pagesize);
        BcNotificationAnnouncement bcNotificationAnnouncement = new BcNotificationAnnouncement();
        bcNotificationAnnouncement.setTitle(title);
        bcNotificationAnnouncement.setCommunityid(communityid);
        List<BcNotificationAnnouncement> list = this.bcNotificationAnnouncementDao.queryAll(bcNotificationAnnouncement);
        PageInfo<BcNotificationAnnouncement> pageInfo = new PageInfo<>(list);
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * @param title 姓名
     * @return
     */
    @Override
    public Result<Object> queryByTitle(String title) {
        Map<String, Object> map = new HashMap<>();
        BcNotificationAnnouncement bcNotificationAnnouncement = new BcNotificationAnnouncement();
        bcNotificationAnnouncement.setTitle(title);
        List<BcNotificationAnnouncement> list = this.bcNotificationAnnouncementDao.queryAll(bcNotificationAnnouncement);
        map.put("NotificationAnnouncement", list);
        map.put("module", "通知公告");
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) list.size(), map);
    }

    /**
     * 新增数据
     *
     * @param bcNotificationAnnouncement 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> insert(BcNotificationAnnouncement bcNotificationAnnouncement) {
        List<BcNotificationAnnouncement> list = this.bcNotificationAnnouncementDao.queryAll(bcNotificationAnnouncement);
        if (list.isEmpty()) {
            if (this.bcNotificationAnnouncementDao.insert(bcNotificationAnnouncement) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "通知公告新增成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通知公告新增失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通知公告已存在！");
    }

    /**
     * 修改数据
     *
     * @param bcNotificationAnnouncement 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> update(BcNotificationAnnouncement bcNotificationAnnouncement) {
        BcNotificationAnnouncement bcNotificationAnnouncementNew = this.bcNotificationAnnouncementDao.queryById(bcNotificationAnnouncement.getId());
        if (bcNotificationAnnouncementNew != null) {
            if (this.bcNotificationAnnouncementDao.update(bcNotificationAnnouncement) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "通知公告更新成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通知公告更新失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通知公告不存在！");
    }

    /**
     * @param bcNotificationAnnouncement 实体
     * @return
     */
    @Override
    public Result<Object> read(BcNotificationAnnouncement bcNotificationAnnouncement) {
        int count = 0;
        BcNotificationAnnouncement bcNotificationAnnouncementNew = this.bcNotificationAnnouncementDao.queryById(bcNotificationAnnouncement.getId());
        if (bcNotificationAnnouncementNew != null) {
            // 假设 getReader() 应该返回 List<Integer>
            String result = JSONArray.toJSONString(bcNotificationAnnouncement.getReader());
            String result1 = (String) bcNotificationAnnouncementNew.getReader();
            List<Integer> list = JSONArray.parseArray(result, Integer.class);
            List<Integer> list1 = JSONArray.parseArray(result1, Integer.class);
            List<Integer> updatedReaders = new ArrayList<>();
            if (list != null && !list.isEmpty() && list1 != null && !list1.isEmpty()) {
                //判断点赞数
                if (bcNotificationAnnouncementNew.getReadcount() < bcNotificationAnnouncement.getReadcount()) {
                    for (Integer readerId : list) {
                        // 可在此处添加具体业务逻辑
                        if (!list1.contains(readerId)) {
                            updatedReaders.add(readerId);
                            count++;
                        }
                    }
                    updatedReaders.addAll(list1);
                    //去重-顺序排序
//                    updatedReaders = updatedReaders.stream().distinct().sorted().collect(Collectors.toList());
                    bcNotificationAnnouncement.setReadcount(bcNotificationAnnouncementNew.getReadcount() + count);
                    bcNotificationAnnouncement.setReader(updatedReaders.toString());
                } else {
                    if (bcNotificationAnnouncementNew.getReadcount() == 0) {
                        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "点赞数不能小于0！");
                    }
                    for (Integer readerId : list) {
                        // 可在此处添加具体业务逻辑
                        if (list1.contains(readerId)) {
                            list1.remove(readerId);
                            count++;
                        }
                    }
                    updatedReaders.addAll(list1);
                    bcNotificationAnnouncement.setReadcount(bcNotificationAnnouncementNew.getReadcount() - count);
                    bcNotificationAnnouncement.setReader(updatedReaders.toString());
                }
            } else {
                bcNotificationAnnouncement.setReadcount(bcNotificationAnnouncementNew.getReadcount() + 1);
                bcNotificationAnnouncement.setReader(list.toString());
            }
            if (this.bcNotificationAnnouncementDao.update(bcNotificationAnnouncement) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "通知公告更新成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通知公告更新失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通知公告不存在！");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result<Object> deleteById(Integer[] id) {
        int count = 0;
        //删除个别
        for (Integer i : id) {
            //删除单个
            if (this.bcNotificationAnnouncementDao.deleteById(i) > 0) {
                count++;
            }
        }
        //判断删除结果
        if (count == id.length) {
            return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) id.length, "通知公告删除成功！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通知公告删除失败！");
    }
}
